package cn.dida.service.impl;

import cn.dida.dao.CompanyDao;
import cn.dida.dao.GeneralSetDao;
import cn.dida.dao.SalesmanCashDao;
import cn.dida.model.Company;
import cn.dida.model.EntPayEntity;
import cn.dida.model.Pageable;
import cn.dida.model.RedPackEntity;
import cn.dida.service.SalesmanCashService;
import cn.dida.util.AliyunMqUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Created by zhangzhiling on 2017/9/1.
 */
@Service
public class SalesmanCashServiceImpl implements SalesmanCashService{

    @Autowired
    private SalesmanCashDao salesmanCashDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private GeneralSetDao generalsetDao;

    /**
     * 提现申请列表
     * @param companyId
     * @param proName
     * @param saleman
     * @param timeLimitStart
     * @param timeLimitEnd
     * @param page
     * @return
     */
    @Override
    public Pageable<Map<String, Object>> queryList(Integer companyId, String proName, String saleman, String timeLimitStart, String timeLimitEnd, Integer page) {
        return salesmanCashDao.queryList(companyId,proName,saleman,timeLimitStart,timeLimitEnd,page);
    }

    /**
     * 提现申请  导出
     * @param companyId
     * @param proName
     * @param saleman
     * @param timeLimitStart
     * @param timeLimitEnd
     * @return
     */
    @Override
    public List<Map<String, Object>> exportList(Integer companyId, String proName, String saleman, String timeLimitStart, String timeLimitEnd) {
        return salesmanCashDao.exportList(companyId,proName,saleman,timeLimitStart,timeLimitEnd);
    }

    /**
     * 支付
     * @param companyId
     * @param cashId
     * @param totalprize
     * @param salesman_id
     * @param openid
     * @return
     */
    @Override
    @Transactional
    public Boolean paymentSave(Integer companyId, Integer cashId, float totalprize,Integer salesman_id, String openid){


        Company company = companyDao.get(companyId);
        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String weixin_qyhmh_id = preference.getString("weixin_mh_id");
        String weixin_qyhmh_pay_key = preference.getString("weixin_mh_pay_key");
        String weixin_qyhmh_apid = preference.getString("weixin_gzh_appid");
        Integer sumprice = (int) (totalprize*10*10);
        String message_id = "";
        EntPayEntity entPay=new EntPayEntity();
        entPay.setCallBackUrl(ResourceBundle.getBundle("app").getString("aliyun.mq.callbackaddr")+"salesmanPayCallback");
        entPay.setAppId(weixin_qyhmh_apid);
        entPay.setMchId(weixin_qyhmh_id);
        entPay.setMchKey(weixin_qyhmh_pay_key);
        entPay.setDescription("企业红包");
        entPay.setOpenId(openid);
        entPay.setAmount(sumprice);

        message_id = AliyunMqUtil.sendEntPayMessage(entPay);

        salesmanCashDao.update_state2(cashId,salesman_id,message_id);
        return null;
    }

    /**
     * 批量支付
     * @param js
     * @param companyId
     * @return
     */
    public Boolean paymentSave_batch_pay(JSONArray js,Integer companyId){

        if(js!=null && js.size()>0) {
            Company company = companyDao.get(companyId);
            JSONObject preference = JSONObject.parseObject(company.getPreference());
            String weixin_qyhmh_id = preference.getString("weixin_mh_id");
            String weixin_qyhmh_pay_key = preference.getString("weixin_mh_pay_key");
            String weixin_qyhmh_apid = preference.getString("weixin_gzh_appid");
            for (Object jo : js) {
                JSONObject genjo = JSONObject.parseObject(jo.toString());

                Integer sumprice = (int) (Float.parseFloat(genjo.getString("totalprize")) * 10 * 10);
                String message_id = "";
                EntPayEntity entPay = new EntPayEntity();
                entPay.setCallBackUrl(ResourceBundle.getBundle("app").getString("aliyun.mq.callbackaddr") + "salesmanPayCallback");
                entPay.setAppId(weixin_qyhmh_apid);
                entPay.setMchId(weixin_qyhmh_id);
                entPay.setMchKey(weixin_qyhmh_pay_key);
                entPay.setDescription("企业红包");
                entPay.setOpenId(genjo.getString("openid"));
                entPay.setAmount(sumprice);

                message_id = AliyunMqUtil.sendEntPayMessage(entPay);

                Integer cashId = Integer.parseInt(genjo.getString("cashId"));
                Integer salesman_id = Integer.parseInt(genjo.getString("salesman_id"));
                salesmanCashDao.update_state2(cashId, salesman_id, message_id);
            }
        }
        return null;
    }

    /**
     * 支付回调
     * @param payresult
     * @param message_id
     * @param descripe
     */
    public void update_callback( Integer payresult,String message_id,String descripe){

        Integer state = 1;
        if(payresult != 0 ){
            state =3;
        }else{
            descripe = null;
        }
        salesmanCashDao.update_callback(state,message_id,descripe);
    }

    /**
     * 已支付列表
     * @param companyId
     * @param proName
     * @param saleman
     * @param timeLimitStart
     * @param timeLimitEnd
     * @param page
     * @return
     */
    @Override
    public Pageable<Map<String, Object>> queryPaidList(Integer companyId, String proName, String saleman, String timeLimitStart, String timeLimitEnd, Integer page) {
        return salesmanCashDao.queryPaidList(companyId,proName,saleman,timeLimitStart,timeLimitEnd,page);
    }

    /**
     * 已支付列表 导出
     * @param companyId
     * @param proName
     * @param saleman
     * @param timeLimitStart
     * @param timeLimitEnd
     * @return
     */
    @Override
    public List<Map<String, Object>> exportPaidList(Integer companyId, String proName, String saleman, String timeLimitStart, String timeLimitEnd) {
        return salesmanCashDao.exportPaidList(companyId,proName,saleman,timeLimitStart,timeLimitEnd);
    }
}
